function initializeUserRate(){
	initializeSectionRate('climate-stars');
	initializeSectionRate('food-stars');
	initializeSectionRate('price-stars');
	initializeSectionRate('service-stars');
}

function initializeUserRateWithValue(food, service, climate, price){
	initializeSectionRate('climate-stars', climate);
	initializeSectionRate('food-stars', food);
	initializeSectionRate('price-stars', price);
	initializeSectionRate('service-stars', service);
	var sum = (food+service+climate+price) / 4
	$('#overall-stars').raty({
	  	  number   : 10,
	  	  readOnly : true,
	  	  half     : true,
	  	  score    : sum,
	  	  size     : 24,
	  	  starHalf : '/images/rates/star-half-big.png',
	  	  starOff  : '/images/rates/star-off-big.png',
	  	  starOn   : '/images/rates/star-on-big.png'
		});
}

function initializeSectionRate(name){
	$('#'+name).raty({ 
		number: 10,
		numberMax:10,
		onClick: setClickTrigger(name)
	});
}
function initializeSectionRate(name, value){
	$('#'+name).raty({ 
		number: 10,
		numberMax:10,
		score : value,
		onClick: setClickTrigger(name)
	});
}

function setClickTrigger(divId){
	$("#"+divId).click(function(){
		var climateRate  = getRate('climate-stars');
		var foodRate = getRate('food-stars');
		var priceRate = getRate('price-stars');
		var serviceRate = getRate('service-stars');
		var sum = (parseInt(0 + climateRate) + parseInt(0 +foodRate) + parseInt(0 + priceRate) + parseInt(0+ serviceRate))/4;
		
		$('#overall-stars').raty({
	    	  number   : 10,
	    	  readOnly : true,
	    	  half     : true,
	    	  score    : sum,
	    	  size     : 24,
	    	  starHalf : '/images/rates/star-half-big.png',
	    	  starOff  : '/images/rates/star-off-big.png',
	    	  starOn   : '/images/rates/star-on-big.png'
	    });
	});
}

function getRate(name){
	return ($('#'+name).raty('score') == null) ? 0 : $('#'+name).raty('score');
}


function sendRate(){
		$("#sidePanel").spin();
		var data = $("#restaurantForm").serializeArray();
		data.push({name: 'climate', value: getRate('climate-stars')});
		data.push({name: 'food', value: getRate('food-stars')});
		data.push({name: 'service', value: getRate('service-stars')});
		data.push({name: 'price', value: getRate('price-stars')});
		$.ajax({
			type : "POST",
			url : '/restaurant/rate',
			data : data,
			success : function(response) {
				$("#sidePanel").spin(false);
				if (response.status == "FAILURE") {
					$.gritter.add({
						title: response.result.title,
						text: response.result.content
					});
				}
				else {
					$.gritter.add({
						title: response.result.title,
						text: response.result.content
					});
				}
			},
			error : function(e) {
				alert(e);
			}
		});
}
